package com.ruoyi.ceshi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ceshi.domain.Fj;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.ceshi.domain.Ceshi;
import com.ruoyi.ceshi.service.ICeshiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 测试Controller
 *
 * @author huacai
 * @date 2024-05-07
 */
@RestController
@RequestMapping("/ceshi/ceshi")
public class CeshiController extends BaseController
{
    @Autowired
    private ICeshiService ceshiService;

    /**
     * 查询测试列表
     */
    @PreAuthorize("@ss.hasPermi('ceshi:ceshi:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ceshi ceshi)
    {
        startPage();
        List<Ceshi> list = ceshiService.selectCeshiList(ceshi);
        return getDataTable(list);
    }

    /**
     * 导出测试列表
     */
    @PreAuthorize("@ss.hasPermi('ceshi:ceshi:export')")
    @Log(title = "测试", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ceshi ceshi)
    {
        List<Ceshi> list = ceshiService.selectCeshiList(ceshi);
        ExcelUtil<Ceshi> util = new ExcelUtil<Ceshi>(Ceshi.class);
        util.exportExcel(response, list, "测试数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Ceshi> util = new ExcelUtil<Ceshi>(Ceshi.class);
        util.importTemplateExcel(response, "测试数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "测试", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ceshi:ceshi:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Ceshi> util = new ExcelUtil<Ceshi>(Ceshi.class);
        InputStream inputStream = file.getInputStream();
        List<Ceshi> list = util.importExcel(inputStream );
        inputStream.close();
        int count = ceshiService.batchInsertCeshi(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取测试详细信息
     */
    @PreAuthorize("@ss.hasPermi('ceshi:ceshi:query')")
    @GetMapping(value = "/{ceshiId}")
    public AjaxResult getInfo(@PathVariable("ceshiId") String ceshiId)
    {
        return success(ceshiService.selectCeshiByCeshiId(ceshiId));
    }

    /**
     * 新增测试
     */
    @PreAuthorize("@ss.hasPermi('ceshi:ceshi:add')")
    @Log(title = "测试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ceshi ceshi)
    {
        return toAjax(ceshiService.insertCeshi(ceshi));
    }

    /**
     * 修改测试
     */
    @PreAuthorize("@ss.hasPermi('ceshi:ceshi:edit')")
    @Log(title = "测试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ceshi ceshi)
    {
        return toAjax(ceshiService.updateCeshi(ceshi));
    }

    /**
     * 删除测试
     */
    @PreAuthorize("@ss.hasPermi('ceshi:ceshi:remove')")
    @Log(title = "测试", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ceshiIds}")
    public AjaxResult remove(@PathVariable String[] ceshiIds)
    {
        return toAjax(ceshiService.deleteCeshiByCeshiIds(ceshiIds));
    }

    /**
     * 获得ID
     */
    @GetMapping("/getCeshiId")
    public AjaxResult getCeshiId(){
        String ceshiId = IdUtils.fastSimpleUUID();
        return success(ceshiId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertCeshiFjByxmsbId(@RequestBody Fj fj)
    {
        return toAjax(ceshiService.insertLxglXmsbFjByxmsbId(fj));
    }

    /**
     * 根据主表ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectLxglXmsbFjList(String sId)
    {
        Fj fj = new Fj();
        fj.setCeshiId(sId);
        return AjaxResult.success(ceshiService.selectLxglXmsbFjList(fj));
    }

}
